<template>
  <div style="margin: 0 10px;">
    <div style="float:right;margin:5px 0px 5px 5px">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="success" plain icon="el-icon-refresh" size="mini" @click="getList">刷新</el-button>
      </el-col>
    </div>

    <el-table v-loading="loading" :data="retrieveconditionList" border>
      <el-table-column label="功能编号" align="center" prop="ctFunNo" />
      <el-table-column label="字段名称" align="center" prop="ctColName" />
      <el-table-column label="字段提示" align="center" prop="ctColTitle" />
      <el-table-column label="关系符" align="center" prop="ctColOperator" />
      <el-table-column label="连接符" align="center" prop="ctColRelation" />
      <el-table-column label="默认初始值" align="center" prop="ctColDefaultv" />
      <el-table-column label="文本框提示" align="center" prop="ctConPlace" />
      <el-table-column label="控件的宽度" align="center" prop="ctConSpan" />
      <el-table-column label="显示顺序" align="center" prop="amSort" />
      <el-table-column
        width="120px"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            title="修改"
          ></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改查询条件对话框  -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-row class="mb8">
        <div style="float:right;">
          <el-button type="primary" size="mini" @click="submitForm" v-if="canSave">保 存</el-button>
        </div>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="字段名称" prop="ctColName">
              <el-input v-model="form.ctColName" placeholder="请输入字段名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="字段提示" prop="ctColTitle">
              <el-input v-model="form.ctColTitle" placeholder="请输入字段提示" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="关系符" prop="ctColOperator">
              <el-select v-model="form.ctColOperator" placeholder="请选择关系符" style="width:100%;">
                <el-option label="大于" value=">" />
                <el-option label="大于等于" value=">=" />
                <el-option label="小于" value="<" />
                <el-option label="小于等于" value="<=" />
                <el-option label="等于" value="=" />
                <el-option label="不等于" value="!=" />
                <el-option label="包含" value="like" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="连接符" prop="ctColRelation">
              <el-select v-model="form.ctColRelation" placeholder="请选择连接符" style="width:100%;">
                <el-option label="与" value="and" />
                <el-option label="或" value="or" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="控件类型" prop="ctConType">
              <el-select v-model="form.ctConType" placeholder="请选择控件类型" style="width:100%;">
                <el-option label="文本" value="tb" />
                <el-option label="日期" value="cd" />
                <el-option label="下拉选择" value="ddlb" />
                <el-option label="树选择" value="popw" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="默认初始值" prop="ctColDefaultv">
              <el-input
                v-model="form.ctColDefaultv"
                placeholder="请输入默认初始值"
                title="日期请输入0day -30day  -90day -365day"
              />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="控件对应的树" prop="ctConTreetype">
              <el-input
                v-model="form.ctConTreetype"
                placeholder="请输入控件对应的树的提取编码"
                title="根据t_mis_tree的treeType确定"
              />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="文本框提示内容" prop="ctConPlace">
              <el-input v-model="form.ctConPlace" placeholder="提示内容" />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="控件的宽度" prop="ctConSpan">
              <el-input v-model="form.ctConSpan" placeholder="请输入自动构建控件的宽度" title="输入的数值是24的倍数" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="显示顺序" prop="amSort">
              <el-input v-model="form.amSort" placeholder="请输入显示顺序" title="输入的数值是5的倍数" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="是否必填" prop="ctConMust">
              <el-switch :active-value="true" :inactive-value="false" v-model="form.ctConMust"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="查询条件显示" prop="ctConDisp">
              <el-switch :active-value="true" :inactive-value="false" v-model="form.ctConDisp"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="使用状态" prop="ctState">
              <el-switch active-value="1" inactive-value="9" v-model="form.ctState"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRetrievecondition,
  getRetrievecondition,
  delRetrievecondition,
  addRetrievecondition,
  updateRetrievecondition,
  exportRetrievecondition,
} from '@/api/mis/retrievecondition'
import { tabeleColumn } from '@/api/mis/function'
import MisSelectTree from '@/components/SpsPublic/treeview/MisSelectTree'
import store from '@/store/index'
import { getFunNo } from '@/utils/party'

export default {
  name: 'Retrievecondition',
  components: {
    MisSelectTree,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询条件表格数据
      retrieveconditionList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        ctConDisp: true,
        ctConMust: false,
      },
      // 表单校验
      rules: {},
      store: store,
      tabeleColumn: [],
      singleStore: {},
      subSingleStore: {},
      funNo: getFunNo(this.$route),
    }
  },
  created() {
    this.singleStore = store.getters.singleStore['main' + this.funNo]
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.queryParams.ctFunNo = this.singleStore.ctId
      this.subSingleStore =
        this.singleStore.subSingleStore['sub' + this.subFunNo]
      this.queryParams.ctFunNo = this.subSingleStore.ctId
      listRetrievecondition(this.queryParams)
        .then((response) => {
          this.retrieveconditionList = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    getTabeleColumn() {
      tabeleColumn(this.store.state.func.tableName).then((response) => {
        this.tabeleColumn = response.data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        ctId: null,
        ctFunNo: null,
        ctColId: null,
        ctColName: null,
        ctColTitle: null,
        ctColOperator: null,
        ctColRelation: null,
        ctColDefaultv: null,
        ctConPlace: null,
        ctConClear: null,
        ctConSpan: null,
        ctConType: null,
        ctConTreetype: null,
        ctConMultiple: null,
        ctConMust: false,
        ctConSearch: null,
        ctChildDisp: true,
        ctChildValue: null,
        ctChildTable: null,
        ctChildFilter: null,
        amSort: null,
        ctState: null,
      }
      this.resetForm('form')
      this.getTabeleColumn()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加显示配置'
      this.reset()
      this.$nextTick(() => {
        this.form.ctFunNo = this.subFunNo
        this.form.ctState = '1'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const ctId = row.ctId || this.ids
      getRetrievecondition(ctId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改查询条件'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.ctId != null) {
            updateRetrievecondition(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRetrievecondition(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ctIds = row.ctId || this.ids
      this.$confirm(
        '是否确认删除查询条件编号为"' + ctIds + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delRetrievecondition(ctIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
  },
}
</script>
